package fr.afcepf.ai89.esharing.managedbean;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

import fr.afcepf.ai89.esharing.entity.Aem;
import fr.afcepf.ai89.esharing.entity.FrDepartement;
import fr.afcepf.ai89.esharing.metier.BusinessApprovisionnerCriteres;
import fr.afcepf.ai89.esharing.metier.BusinessRechercheAem;
import fr.afcepf.ai89.esharing.metier.IBusinessApprovisionnerCriteres;
import fr.afcepf.ai89.esharing.metier.IBusinessRechercheAem;

public class IndexManagedBean {
	
	private String motCleRecherche = null;
	private Date dateDebut = null;
	private Date dateFin = null;
	private String avisC1 = "";
	private String avisC2 = "";
	private SelectItem[] departements;
	private String departementId = null;
	private IBusinessApprovisionnerCriteres busApp = new BusinessApprovisionnerCriteres();
	private IBusinessRechercheAem busRec = new BusinessRechercheAem();
	private String lblErr = "";
	private boolean onThisPage;
	
	public boolean isOnThisPage() {
		return onThisPage;
	}

	public void setOnThisPage(boolean onThisPage) {
		this.onThisPage = onThisPage;
	}

	private void loadDepartements() {
		List<FrDepartement> liste = busApp.getDepts();
		int i = 1;
		departements = new SelectItem[liste.size() + 1];
		departements[0] = new SelectItem();
		departements[0].setLabel("Dépt.");
		departements[0].setValue("");
		for (FrDepartement dept : liste) {
			departements[i] = new SelectItem();
			
			departements[i].setLabel(String.valueOf(dept.getIdDepartement()) + " " + dept.getNomDepartement());
			departements[i].setValue(dept.getIdDepartement());
			i++;
		}
	}
	
	public String getAvisC1() {
		return avisC1;
	}

	public void setAvisC1(String avisC1) {
		this.avisC1 = avisC1;
	}

	public String getAvisC2() {
		return avisC2;
	}

	public void setAvisC2(String avisC2) {
		this.avisC2 = avisC2;
	}

	public SelectItem[] getDepartements() {
		return departements;
	}

	public void setDepartements(SelectItem[] departements) {
		this.departements = departements;
	}

	public IBusinessApprovisionnerCriteres getBusApp() {
		return busApp;
	}

	public void setBusApp(IBusinessApprovisionnerCriteres busApp) {
		this.busApp = busApp;
	}

	public String getDepartementId() {
		return departementId;
	}




	public String getLblErr() {
		return lblErr;
	}

	public void setLblErr(String lblErr) {
		this.lblErr = lblErr;
	}

	public void setDepartementId(String departementId) {
		this.departementId = departementId;
	}
	
	
	public String getMotCleRecherche() {
		return motCleRecherche;
	}

	public void setMotCleRecherche(String motCleRecherche) {
		this.motCleRecherche = motCleRecherche;
	}

	public Date getDateDebut() {
		return dateDebut;
	}

	public void setDateDebut(Date dateDebut) {
		this.dateDebut = dateDebut;
	}

	public Date getDateFin() {
		return dateFin;
	}

	public void setDateFin(Date dateFin) {
		this.dateFin = dateFin;
	}
	
	public IndexManagedBean() {
		
		loadDepartements();
		
	}
	
	public String rechercher() {
		
		onThisPage = true;
		
		if (motCleRecherche != "" || departementId != "" || dateDebut != null || dateFin != null) {
			
			return "resultatrecherche";
		
		}
		else {
			
			lblErr = "Veuillez renseigner au moins un critère de recherche.";
			
		}
		
		return "";
		
	}
	
}
